package com.zt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zt.globalExceptionHandler.BizException;
import com.zt.mapper.StudentMapper;
import com.zt.pojo.PageResult;
import com.zt.pojo.Student;
import com.zt.pojo.StudentModel;
import com.zt.pojo.StudentsQuery;
import com.zt.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    //分页查询
    @Override
    public PageResult QueryPage(StudentsQuery studentsQuery) {
        //设置页码
        PageHelper.startPage(studentsQuery.getPage(), studentsQuery.getPageSize());
        //执行查询
        List<Student> studentsQueryResult = studentMapper.QueryPage(studentsQuery);
        //封装分页结果
        Page<Student> p = (Page<Student>)studentsQueryResult;
        return new PageResult(p.getTotal(), p.getResult());
    }
    //删除学员
    @Override
    public void deleteById(Integer[] id) {
        studentMapper.deleteById(id);
    }

    //添加学员
    @Override
    public void add(Student student) {
        //补全信息
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(student.getCreateTime());
        studentMapper.add(student);
    }

    //根据id查询学员
    @Override
    public Student findById(Integer id) {
        return studentMapper.findById(id);
    }

    //修改学员信息
    @Override
    public void update(Student student) {
        //补全信息
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    //违纪
    @Override
    public void updatescore(Integer id, Integer score) {
        //进行判断，如果传入的参数为负数则返回Result.error("违纪分数不能为负数");
        if (score < 0) {
            throw new BizException("违纪扣分不能为负数");
        }else if(score > 40){
            throw new BizException("单次违纪扣分不能超过40分");
        }else if(score == 0){
            throw new BizException("请输入要扣除的分数，不能扣0分");
        }
        else {
            //补全信息,根据id 查询出该学员
            Student student = studentMapper.findById(id);
            //得到违纪次数并加一赋值给违纪次数
            int violationCount = student.getViolationCount() + 1;
            student.setViolationCount(violationCount);
            //得到违纪分数并加上本次违纪分数
            int violationScore = student.getViolationScore() + score;
            student.setViolationScore(violationScore);
            //得到更新时间
            student.setUpdateTime(LocalDateTime.now());
            //更新
            studentMapper.update(student);
        }
    }

    @Override
    public void importStudent(Integer clazzId, List<StudentModel> dataList) {
        if(!dataList.isEmpty()){
            List<Student> students = dataList.stream().map(data -> {
                Student student = BeanUtil.copyProperties(data, Student.class);
                student.setGender(data.getGenderStr().equals("男") ? 1 : 2);
                student.setIsCollege(data.getIsCollegeStr().equals("是") ? 1 : 0);
                switch (data.getDegreeStr()){
                    case "高中":
                        student.setDegree(2);
                        break;
                    case "大专":
                        student.setDegree(3);
                        break;
                    case "本科":
                        student.setDegree(4);
                        break;
                    case "硕士":
                        student.setDegree(5);
                        break;
                    case "博士":
                        student.setDegree(6);
                        break;
                    default:
                       student.setDegree(1);
                }
                student.setClazzId(clazzId);
                student.setCreateTime(LocalDateTime.now());
                student.setUpdateTime(LocalDateTime.now());
                return student;
            }).toList();
             studentMapper.importStudent(students);
        }
    }

    @Override
    public List<StudentModel> list(String name, Integer degree, Integer clazzId) {
        StudentsQuery studentsQuery = new StudentsQuery(name, degree, clazzId, null, null);
        List<Student> studentList = studentMapper.QueryPage(studentsQuery);
        if(!studentList.isEmpty()){
            return studentList.stream().map(student -> {
                StudentModel sm = BeanUtil.copyProperties(student, StudentModel.class);
                sm.setGenderStr(student.getGender() == 1 ? "男" : "女");
                sm.setIsCollegeStr(student.getIsCollege() == 1 ? "是" : "否");
                switch (student.getDegree()) {
                    case 1 -> sm.setDegreeStr("初中");
                    case 2 -> sm.setDegreeStr("高中");
                    case 3 -> sm.setDegreeStr("大专");
                    case 4 -> sm.setDegreeStr("本科");
                    case 5 -> sm.setDegreeStr("硕士");
                    case 6 -> sm.setDegreeStr("博士");
                    default -> sm.setDegreeStr("未知");
                }
                return sm;
            }).toList();
        }
        return new ArrayList<>();
    }
}
